package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.systemLogCommand.AddSystemLogCommand;
import com.woniu.command.systemLogCommand.DeleteSystemLogCommand;
import com.woniu.command.systemLogCommand.UpdateSystemLogCommand;
import com.woniu.inlet.vo.SystemLogVo;
import com.woniu.outlet.po.SystemLogPo;
import com.woniu.service.SystemLogService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "系统日志管理")
@Slf4j
@RequestMapping("/systemLog/")
public class SystemLogController {

    @Autowired
    private SystemLogService systemLogService;


    @GetMapping("/systemLogs")
    @ApiOperation("查询系统日志")
    public ResponseResult<List<SystemLogVo>> findSystemLogs() throws Exception{
        List<SystemLogPo>  systemLogPos =  systemLogService.findSystemLogs();
        List<SystemLogVo> systemLogVos = new ArrayList<>();
        //对象的深拷贝
        for (SystemLogPo item : systemLogPos){
            SystemLogVo systemLogVo = new SystemLogVo();
            BeanUtils.copyProperties(item,systemLogVo);
            systemLogVos.add(systemLogVo);
        }

        addSystemLog("查询系统日志");
        return new ResponseResult<>(systemLogVos,"查询成功", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/systemLogs/id")
    @ApiOperation("根据id查询系统日志")
    public ResponseResult<SystemLogVo> getSystemLogById(Long id) throws Exception {
        SystemLogPo systemLogPo = systemLogService.getSystemLogById(id);

        //对象的深拷贝

        SystemLogVo systemLogVo = new SystemLogVo();
        BeanUtils.copyProperties(systemLogPo, systemLogVo);

        addSystemLog("根据id查询系统日志");
        return new ResponseResult<>(systemLogVo, "查询成功", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加系统日志")
    @ApiOperationSupport(ignoreParameters = {"id","operationDate","loginUser","spare1","spare2","spare3"})
    public ResponseResult<String> addSystemLog(String systemClassificationName){
        SystemLogPo systemLogPo = new SystemLogPo();
        systemLogPo.setSystemClassificationName(systemClassificationName);
        Command command = new AddSystemLogCommand();
        command.execute(systemLogPo);

        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改系统日志")
    @ApiOperationSupport(ignoreParameters = {"operationDate","spare1","spare2","spare3"})
    public ResponseResult<String> updateSystemLog(SystemLogPo systemLogPo){


        Command command = new UpdateSystemLogCommand();
        command.execute(systemLogPo);
        addSystemLog("修改系统日志");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除系统日志")
    public ResponseResult<String> deleteSystemLog(Long id){
        Command command = new DeleteSystemLogCommand();
        command.execute(id);
        addSystemLog("根据id删除系统日志");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
